#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/input.h>

static struct input_event event;
static int version;
static int fd;

int main(int argc, char *argv[])
{
    ssize_t ret;

    if(argc != 5) {
        printf("use: %s device type code value\n", argv[0]);
        return -1;
    }

    memset(&event, 0, sizeof(event));

    event.type = atoi(argv[2]);
    event.code = atoi(argv[3]);
    event.value = atoi(argv[4]);

    fd = open(argv[1], O_RDWR);

    if (fd < 0) {
        fprintf(stderr, "open failed\n");
        return errno;
    }

    if (ioctl(fd, EVIOCGVERSION, &version)) {
        printf("get driver version failed\n");
        return errno;
    }

    ret = write(fd, &event, sizeof(event));

    if (ret < (ssize_t) sizeof(event)) {
        printf("write event failed, %s\n", strerror(errno));
        return errno;
    }

    return 0;
}
